import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/home/HomeView.vue'
import Login from '../views/Login.vue'
import Detail from '../views/Detail.vue'
import Text from '../views/home/components/Text.vue'
import Mine from '../views/home/components/Mine.vue'
import Add from '../views/home/components/Add.vue'
const router = createRouter({
  history:  createWebHistory(),
  routes: [
    {
      path: '/login',
      name: 'login',
      component: Login
    },
    {path: '/',redirect:'/home'},
    {
      path:'/home',
      name: 'home',
      component: HomeView,
      meta:{
        isLogin:true
      },
      children:
      [
        {
          path: '/home/text',
          name: 'text',
          component: Text,
          meta:{
            isLogin:true
          }
        },
        {
          path: '/home/add',
          name: 'add',
          component: Add,
          meta:{
            isLogin:true
          }
        },
        {
          path: '/home/mine',
          name: 'mine',
          component: Mine,
          meta:{
            isLogin:true
          }
        }
      ]
    },
    {
      path: '/detail',
      name: 'detail',
      component: Detail,
      meta:{
        isLogin:true
      }
    }
  ],
})


// 路由首位 
router.beforeEach((to,from,next) => {
  if(to.meta.isLogin) {
    const token = localStorage.getItem('token')
    if(!token) {
      next ({
        name:'login',
        params:{
          redirectUrl: to.fullPath
        }
      })
      return
    }
  }
  next()
})



export default router
